package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.Communication;

import org.apache.jasper.tagplugins.jstl.core.ForEach;


public class SearchData {
	public static ArrayList<String> searchFriend(String email){
		ArrayList<String> list = new ArrayList<>();
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM user WHERE email like '%"+email+"%' ";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
//			pre.setString(1, email);
			  ResultSet rs = pre.executeQuery();
			while(rs.next()){
				String emails = rs.getString("email");
				list.add(emails);
				
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
		
	}
	public static ArrayList<Communication> searchContent(String title){
		
		ArrayList<Communication> list = new ArrayList<>();
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM communication WHERE title like '%"+title+"%' ";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
//			pre.setString(1, email);
			  ResultSet rs = pre.executeQuery();
			while(rs.next()){
				long timeContent = rs.getLong("timeContent");
				String link = rs.getString("link");
				String title1 = rs.getString("title");
				int type = rs.getInt("type");
				String email = rs.getString("user_email");
				Communication com = new Communication(title1, link, timeContent,type,email);
				list.add(com);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
		
	}
	
	public static void main(String[] args) {
		ArrayList<String> list = searchFriend("hoang");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}
}
